<template>
  <div>
    <el-descriptions style="margin-left: 200px" title="个人信息" :column="1">
      <el-descriptions-item label="用户名">{{ user.username }}</el-descriptions-item>
      <el-descriptions-item label="昵称">{{ user.nickname }}</el-descriptions-item>
      <el-descriptions-item label="手机号">{{ user.phone }}</el-descriptions-item>
      <el-descriptions-item label="备注">
        <el-tag size="small">无</el-tag>
      </el-descriptions-item>
    </el-descriptions>
    <div style="margin-left: 150px">
      <el-button @click="openUpdateInfo()" type="warning" plain>修改个人信息</el-button>
      <el-button @click="openUpdatePassword()" type="warning" plain>修改密码</el-button>
    </div>


    <el-dialog top="200px" title="修改个人信息" width="500px" :visible.sync="updateInfoFormVisible">
      <el-form :model="ruleForm" :rules="rulesInfo" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input :disabled="true" style="width: 300px" v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input style="width: 300px" v-model="ruleForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input style="width: 300px" v-model="ruleForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelUpdateInfo()">取消修改</el-button>
        <el-button type="warning" @click="submitUpdateInfo()">确认修改</el-button>
      </div>
    </el-dialog>

    <el-dialog top="200px" title="修改密码" width="500px" :visible.sync="updatePasswordFormVisible">
      <el-form :model="passwordForm" :rules="rulesPassword" label-width="100px" class="demo-ruleForm">
        <el-form-item label="原密码" prop="oldPassword">
          <el-input style="width: 300px" v-model="passwordForm.oldPassword"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input style="width: 300px" v-model="passwordForm.newPassword"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelUpdatePassword()">取消修改</el-button>
        <el-button type="warning" @click="submitUpdatePassword()">确认修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import HomeView from "@/views/HomeView";
export default {
  components:{
    HomeView
  },
  data() {
    return {
      user: {},
      ruleForm: {
        username: '',
        nickname: '',
        phone: ''
      },
      passwordForm:{
        oldPassword: '',
        newPassword:''
      },
      updateInfoFormVisible: false,
      updatePasswordFormVisible:false,
      rulesInfo:{
        nickname:[
          {required: true, message: '请输入昵称', trigger: 'blur'}
        ],
        phone:[
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          {pattern: /^1[3-9]\d{9}$/, message: '请输入11位手机号码', trigger: 'blur'}
        ],
      },
      rulesPassword:{
        oldPassword:[
          {required: true, message: '请输入原密码', trigger: 'blur'}
        ],
        newPassword:[
          {required: true, message: '请输入新密码', trigger: 'blur'},
          {pattern: /(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*()_.]+)$)^[\w~!@#$%^&*()_.]{8,16}$/,
            message: '密码应为字母，数字，特殊符号，两种及以上组合，8-16位', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    updateLogout(){
      HomeView.methods.logout();
      console.log(HomeView.data().isLogin);
    },
    submitUpdatePassword(){
      let url = 'http://localhost:9580/users/user/updateUserPassword';

      let formData = this.qs.stringify(this.passwordForm);

      console.log('url' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            showClose: true,
            message: '修改密码成功！',
            type: 'success'
          });
          this.$router.push("/");
          this.updateLogout();
          this.updatePasswordFormVisible = false;

        } else {
          this.$message({
            showClose: true,
            message: responseBody.message,
            type: 'error'
          });
        }
      });
    },
    cancelUpdatePassword(){
      this.updatePasswordFormVisible=false;
      this.passwordForm.newPassword='';
      this.passwordForm.oldPassword='';
    },
    openUpdatePassword(){
      this.updatePasswordFormVisible=true;
    },
    submitUpdateInfo() {
      let url = 'http://localhost:9580/users/user/updateInfoById';

      let formData = this.qs.stringify(this.ruleForm);

      console.log('url' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            showClose: true,
            message: '修改个人信息成功！',
            type: 'success'
          });
          this.updateInfoFormVisible = false;
          this.loadUser();
        } else {
          this.$message({
            showClose: true,
            message: responseBody.message,
            type: 'error'
          });
        }
      });
    },
    cancelUpdateInfo() {
      this.ruleForm.username = this.user.username;
      this.ruleForm.nickname = this.user.nickname;
      this.ruleForm.phone = this.user.phone;
      this.updateInfoFormVisible = false;
    },
    openUpdateInfo() {
      this.ruleForm.username = this.user.username;
      this.ruleForm.nickname = this.user.nickname;
      this.ruleForm.phone = this.user.phone;
      console.log(this.user);
      this.updateInfoFormVisible = true;
    },
    loadUser() {
      let url = 'http://localhost:9580/users/user';

      console.log('url' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.user = responseBody.data;
        console.log(this.user);
      });
    }
  },
  mounted() {
    this.loadUser();
  }
}
</script>

<style scoped>

</style>